import { Component, OnInit } from '@angular/core';
import { News } from '../../model/news';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.css']
})
export class DetailComponent implements OnInit {

  news:News;
  parm:string;

  constructor(
    private newsservice:NewsService,
    private router:Router,
    private routerinfo:ActivatedRoute
  ) {
    this.news=new News();
   }

  ngOnInit() {
    this.parm=this.routerinfo.snapshot.params["id"];
    if(this.parm){
      this.getSingleNews(this.parm);
    }
    else{
      alert("参数异常，请重试");
      this.goBack();
    }
  }

  getSingleNews(id:string){
    this.newsservice.getSingleNews(id)
    .then((data:News)=>{
      if(data){
        this.news=data;
      }
      else{
        alert("通讯异常，请刷新后重试")
      }
    })
  }

  goBack(){

    this.router.navigate(['managelistforuser']);

  }


}
